package com.ruoyi.supervise.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.supervise.domain.ConDrawFirstTrial;
import com.ruoyi.supervise.domain.ConDrawing;
import com.ruoyi.supervise.domain.ConDrawingTable;
import com.ruoyi.supervise.domain.OpinionCauseContent;
import com.ruoyi.supervise.domain.ProReviewPassRefuse;
import com.ruoyi.supervise.domain.RelatedDesignUnits;
import com.ruoyi.supervise.domain.WDDesignPersonInfo;
import com.ruoyi.supervise.domain.WDOpinionInfo;
import com.ruoyi.supervise.domain.WDUnitProjectInfo;
import com.ruoyi.supervise.domain.WdBaseCase;
import com.ruoyi.supervise.mapper.ConDrawingMapper;
import com.ruoyi.supervise.mapper.ConstructionPermitMapper;
import com.ruoyi.supervise.mapper.ProjectArchivesBaseInfoMapper;
import com.ruoyi.supervise.service.IConDrawingService;
import com.ruoyi.supervise.util.FieldStrUtils;
import com.ruoyi.supervise.util.XmlReadUtil;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;

/**
 * @ClassName: ConDrawingServiceImpl
 * @Description: 施工图审查impl
 * @Author wtj
 * @DateTime 2020年3月19日 上午10:00:38
 */
@Service
@DataSource(value = DataSourceType.SLAVE)
public class ConDrawingServiceImpl implements IConDrawingService 
{
    @Autowired
    private ConDrawingMapper conDrawingMapper;
    
    @Autowired
    private ConstructionPermitMapper constructionPermitMapper;
    
    @Autowired
    private ProjectArchivesBaseInfoMapper projectArchivesBaseInfoMapper;

	@Override
	public List<ConDrawingTable> selectConDrawingTableList(ConDrawingTable conDrawingTable) {
		if(StringUtils.isEmpty(conDrawingTable.getBeginReportDate()) && !StringUtils.isEmpty(conDrawingTable.getEndReportDate())) {
			conDrawingTable.setBeginReportDate("1970-01-01");
		}else if(!StringUtils.isEmpty(conDrawingTable.getBeginReportDate()) && StringUtils.isEmpty(conDrawingTable.getEndReportDate())) {
			conDrawingTable.setEndReportDate(DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN));
		}
		if(StringUtils.isEmpty(conDrawingTable.getBeginProceduralCheakTime()) && !StringUtils.isEmpty(conDrawingTable.getEndProceduralCheakTime())) {
			conDrawingTable.setBeginProceduralCheakTime("1970-01-01");
		}else if(!StringUtils.isEmpty(conDrawingTable.getBeginProceduralCheakTime()) && StringUtils.isEmpty(conDrawingTable.getEndProceduralCheakTime())) {
			conDrawingTable.setEndProceduralCheakTime(DateUtil.format(new Date(), DatePattern.NORM_DATE_PATTERN));
		}
		List<ConDrawingTable> conDrawingTableList=conDrawingMapper.selectConDrawingTableList(conDrawingTable);
		if(!StringUtils.isEmpty(conDrawingTableList)) {
			RelatedDesignUnits rdu=new RelatedDesignUnits();
			   rdu.setGuid("Guid");
			   rdu.setName("Name");
			   rdu.setLinkMan("LinkMan");
			   rdu.setLinkTel("LinkTel");
			   rdu.setLinkEMail("LinkEMail");
			   rdu.setDesignContractAmount("DesignContractAmount");
			   rdu.setLeadMan("LeadMan");
			   rdu.setCertLevel("CertLevel");
			   rdu.setCertNum("CertNum");
			   rdu.setPerforms("Performs");
			   rdu.setPerformsIDCard("PerformsIDCard");
			   rdu.setProjectDir("ProjectDir");
			   rdu.setProjectDirIDCard("ProjectDirIDCard");
			   rdu.setDean("Dean");
			   rdu.setDeanIDCard("DeanIDCard");
			   String nodePath = "/PsDataPacket/FDataList/RelevantUnit";
			for(ConDrawingTable cdt:conDrawingTableList) {
				if(!StringUtils.isEmpty(cdt.getRelatedBuildUnits())) {
					cdt.setRelatedBuildUnitsList(XmlReadUtil.getRelatedDesignUnitsXml(cdt.getRelatedBuildUnits(), nodePath, rdu));
				}
				if(!StringUtils.isEmpty(cdt.getRelatedProspectUnits())) {
					cdt.setRelatedProspectUnitsList(XmlReadUtil.getRelatedDesignUnitsXml(cdt.getRelatedProspectUnits(), nodePath, rdu));
				}
				if(!StringUtils.isEmpty(cdt.getRelatedDesignUnits())) {
					cdt.setRelatedDesignUnitsList(XmlReadUtil.getRelatedDesignUnitsXml(cdt.getRelatedDesignUnits(), nodePath, rdu));
				}
			}
		}
		return conDrawingTableList;
	}

	@Override
	public WdBaseCase selectWdBaseCaseByGuid(String guid) {
		// TODO Auto-generated method stub
		return conDrawingMapper.selectWdBaseCaseByGuid(guid);
	}

	@Override
	public ConDrawing selectConDrawingByGuid(String guid) {
		ConDrawing cd=conDrawingMapper.selectConDrawingByGuid(guid);
		if(cd!=null) {
			//查询出老数据设计单位人员信息
			cd.setLicenseNumImage(FieldStrUtils.fileField(cd.getLicenseNumImage()));
			WDDesignPersonInfo dpi=new WDDesignPersonInfo();
			dpi.setWorkDraingGuid(guid);
			dpi.setDesignerCompanyGuid(cd.getDesignUnitGuid());
			cd.setWdDesinerList(conDrawingMapper.selectWDDesPerByWorkDraGuidAndDeComGuid(dpi));
			//把老数据的建设单位添加到relatedBuildUnitsList集合中
			List<RelatedDesignUnits> relatedBuildUnitsList = new ArrayList<RelatedDesignUnits>();
			RelatedDesignUnits relatedBuildUnits = new RelatedDesignUnits();
			if(!StringUtils.isEmpty(cd.getBuildGuid()) || !StringUtils.isEmpty(cd.getBuildName()) ||  
					!StringUtils.isEmpty(cd.getBuildLinkMan()) || !StringUtils.isEmpty(cd.getBuildPhone()) || 
					!StringUtils.isEmpty(cd.getBuildLinkEmail())) {
				relatedBuildUnits.setGuid(cd.getBuildGuid());
				relatedBuildUnits.setName(cd.getBuildName());
				relatedBuildUnits.setLinkMan(cd.getBuildLinkMan());
				relatedBuildUnits.setLinkTel(cd.getBuildPhone());
				relatedBuildUnits.setLinkEMail(cd.getBuildLinkEmail());
				relatedBuildUnitsList.add(relatedBuildUnits);
			}
			//把老数据的勘察单位添加到relatedProspectUnitsList集合中
			List<RelatedDesignUnits> relatedProspectUnitsList = new ArrayList<RelatedDesignUnits>();
			RelatedDesignUnits relatedProspectUnits = new RelatedDesignUnits();
			if(!StringUtils.isEmpty(cd.getProspectUnitGuid()) || !StringUtils.isEmpty(cd.getProspectUnitName()) ||  
					!StringUtils.isEmpty(cd.getProsCropCert()) ) {
				relatedProspectUnits.setGuid(cd.getProspectUnitGuid());
				relatedProspectUnits.setName(cd.getProspectUnitName());
				relatedProspectUnits.setCertLevel(cd.getProsCropCert());
				relatedProspectUnitsList.add(relatedProspectUnits);
			}
			//把老数据的设计单位添加到relatedDesignUnitsList集合中
			List<RelatedDesignUnits> relatedDesignUnitsList = new ArrayList<RelatedDesignUnits>();
			RelatedDesignUnits relatedDesignUnits = new RelatedDesignUnits();
			if(!StringUtils.isEmpty(cd.getDesignUnitGuid()) || !StringUtils.isEmpty(cd.getDesignUnitName()) ||  
					!StringUtils.isEmpty(cd.getDesignCropCert()) || !StringUtils.isEmpty(cd.getDesignUnitLegalRep()) || 
					!StringUtils.isEmpty(cd.getDesignUnitLinkMan()) || !StringUtils.isEmpty(cd.getDesignUnitLinkPhone()) || 
					!StringUtils.isEmpty(cd.getDesignUnitLinkEmail()) || 
					!StringUtils.isEmpty(cd.getPerforms()) || !StringUtils.isEmpty(cd.getPerformsIDCard()) || 
					!StringUtils.isEmpty(cd.getProjectDir()) || !StringUtils.isEmpty(cd.getProjectDirIDCard()) || 
					!StringUtils.isEmpty(cd.getDean()) || !StringUtils.isEmpty(cd.getDeanIDCard()) || 
					!StringUtils.isEmpty(cd.getWdDesinerList()) || (!StringUtils.isEmpty(cd.getDesignContractAmount()) && Double.parseDouble(cd.getDesignContractAmount()) != 0)) {
				relatedDesignUnits.setGuid(cd.getDesignUnitGuid());
				relatedDesignUnits.setName(cd.getDesignUnitName());
				relatedDesignUnits.setCertLevel(cd.getDesignCropCert());
				relatedDesignUnits.setLeadMan(cd.getDesignUnitLegalRep());
				relatedDesignUnits.setLinkMan(cd.getDesignUnitLinkMan());
				relatedDesignUnits.setLinkTel(cd.getDesignUnitLinkPhone());
				relatedDesignUnits.setLinkEMail(cd.getDesignUnitLinkEmail());
				relatedDesignUnits.setDesignContractAmount(cd.getDesignContractAmount());
				relatedDesignUnits.setPerforms(cd.getPerforms());
				relatedDesignUnits.setPerformsIDCard(cd.getPerformsIDCard());
				relatedDesignUnits.setProjectDir(cd.getProjectDir());
				relatedDesignUnits.setProjectDirIDCard(cd.getProjectDirIDCard());
				relatedDesignUnits.setDean(cd.getDean());
				relatedDesignUnits.setDeanIDCard(cd.getDeanIDCard());
				relatedDesignUnits.setWdDesinerList(cd.getWdDesinerList());
				relatedDesignUnitsList.add(relatedDesignUnits);
			}
			//从xml中取出新数据的勘察，建设，设计单位的信息
			RelatedDesignUnits rdu=new RelatedDesignUnits();
			   rdu.setGuid("Guid");
			   rdu.setName("Name");
			   rdu.setLinkMan("LinkMan");
			   rdu.setLinkTel("LinkTel");
			   rdu.setLinkEMail("LinkEMail");
			   rdu.setDesignContractAmount("DesignContractAmount");
			   rdu.setLeadMan("LeadMan");
			   rdu.setCertLevel("CertLevel");
			   rdu.setCertNum("CertNum");
			   rdu.setPerforms("Performs");
			   rdu.setPerformsIDCard("PerformsIDCard");
			   rdu.setProjectDir("ProjectDir");
			   rdu.setProjectDirIDCard("ProjectDirIDCard");
			   rdu.setDean("Dean");
			   rdu.setDeanIDCard("DeanIDCard");
			   String nodePath = "/PsDataPacket/FDataList/RelevantUnit";
			   if(!StringUtils.isEmpty(cd.getRelatedBuildUnits())) {
				   if(!StringUtils.isEmpty(XmlReadUtil.getRelatedDesignUnitsXml(cd.getRelatedBuildUnits(), nodePath, rdu))) {
					   for(RelatedDesignUnits r:XmlReadUtil.getRelatedDesignUnitsXml(cd.getRelatedBuildUnits(), nodePath, rdu)) {
						   relatedBuildUnitsList.add(r);
					   }
				   }
			   }
			   if(!StringUtils.isEmpty(cd.getRelatedProspectUnits())) {
				   if(!StringUtils.isEmpty(XmlReadUtil.getRelatedDesignUnitsXml(cd.getRelatedProspectUnits(), nodePath, rdu))) {
					   for(RelatedDesignUnits r:XmlReadUtil.getRelatedDesignUnitsXml(cd.getRelatedProspectUnits(), nodePath, rdu)) {
						   relatedProspectUnitsList.add(r);
					   }
				   }
			   }
			   if(!StringUtils.isEmpty(cd.getRelatedDesignUnits())) {
				   if(!StringUtils.isEmpty(XmlReadUtil.getRelatedDesignUnitsXml(cd.getRelatedDesignUnits(), nodePath, rdu))) {
					   for(RelatedDesignUnits r:XmlReadUtil.getRelatedDesignUnitsXml(cd.getRelatedDesignUnits(), nodePath, rdu)) {
						   dpi.setDesignerCompanyGuid(r.getGuid());
						   r.setWdDesinerList(conDrawingMapper.selectWDDesPerByWorkDraGuidAndDeComGuid(dpi));
						   relatedDesignUnitsList.add(r);
					   }
				   }
			   }
			   cd.setRelatedBuildUnitsList(relatedBuildUnitsList);
			   cd.setRelatedProspectUnitsList(relatedProspectUnitsList);
			   cd.setRelatedDesignUnitsList(relatedDesignUnitsList);
		}
		return cd;
	}

	@Override
	public List<WDUnitProjectInfo> selectWDUnitProjectInfoByProRevGuid(String guid) {
		// TODO Auto-generated method stub
		return conDrawingMapper.selectWDUnitProjectInfoByProRevGuid(guid);
	}

	@Override
	public List<WDDesignPersonInfo> selectWDDesPerByWorkDraGuidAndDeComGuid(WDDesignPersonInfo dpi) {
		// TODO Auto-generated method stub
		return conDrawingMapper.selectWDDesPerByWorkDraGuidAndDeComGuid(dpi);
	}

	@Override
	public List<WDOpinionInfo> selectWDOpinionInfoByDataGuid(String dataGuid) {
		List<WDOpinionInfo> wdoiList = conDrawingMapper.selectWDOpinionInfoByDataGuid(dataGuid);
		for(WDOpinionInfo wdoi:wdoiList) {
			if("上报".equals(wdoi.getRole())) {
				wdoi.setCauseContent(" ");
			}else if("程序性审查通过".equals(wdoi.getRole()) || "程序性审查退回".equals(wdoi.getRole())) {
//				WDOpinionInfo wdois = new WDOpinionInfo();
//				wdois.setDataGuid(dataGuid);
//				wdois.setPreviousStateGuid(wdoi.getPreviousStateGuid());
//				wdois.setRole(wdoi.getRole());
//				WDOpinionInfo wdoa = conDrawingMapper.selectWDOpinionInfoByPreviousStateGuid(wdois);
				WDOpinionInfo wdoa = conDrawingMapper.selectWDOpinionInfoByGuid(wdoi.getPreviousStateGuid());
				if(!StringUtils.isNull(wdoa) && !StringUtils.isEmpty(wdoa.getOperateDate()) && !StringUtils.isEmpty(wdoi.getOperateDate())) {
					wdoi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(wdoa.getOperateDate()), DateUtil.parseDateTime(wdoi.getOperateDate()), DateUnit.DAY)));
				}else {
					wdoi.setCauseContent("0");
				}
			}else if("整改上报".equals(wdoi.getRole())) {
				WDOpinionInfo wdois = new WDOpinionInfo();
				wdois.setDataGuid(dataGuid);
				//wdois.setRole("指定审查人员");
				wdois.setRole("程序性审查退回");
				wdois.setCheckCount(wdoi.getCheckCount());
				WDOpinionInfo wdoa = conDrawingMapper.selectWDOpinionInfoByPreviousStateGuid(wdois);
				if(!StringUtils.isNull(wdoa) && !StringUtils.isEmpty(wdoa.getOperateDate()) && !StringUtils.isEmpty(wdoi.getOperateDate())) {
					wdoi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(wdoa.getOperateDate()), DateUtil.parseDateTime(wdoi.getOperateDate()), DateUnit.DAY)));
				}else {
					wdoi.setCauseContent("0");
				}
			}else if("指定审查人员".equals(wdoi.getRole())) {
				WDOpinionInfo result = rectReportReturn(wdoiList,wdoi);
				if(StringUtils.isNotNull(result)) {
					wdoi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(result.getOperateDate()), DateUtil.parseDateTime(wdoi.getOperateDate()), DateUnit.DAY)));
				}else {
					wdoi.setCauseContent("0");
				}
			}else if("审查人员复审".equals(wdoi.getRole()) || "审查人员初审".equals(wdoi.getRole())) {
				WDOpinionInfo wdois = new WDOpinionInfo();
				wdois.setDataGuid(dataGuid);
				wdois.setRole("指定审查人员");
				wdois.setCheckCount(wdoi.getCheckCount());
				WDOpinionInfo wdoa = conDrawingMapper.selectWDOpinionInfoByPreviousStateGuid(wdois);
				if(!StringUtils.isNull(wdoa) && !StringUtils.isEmpty(wdoa.getOperateDate()) && !StringUtils.isEmpty(wdoi.getOperateDate())) {
					wdoi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(wdoa.getOperateDate()), DateUtil.parseDateTime(wdoi.getOperateDate()), DateUnit.DAY)));
				}else {
					wdoi.setCauseContent("0");
				}
			}else if("技术负责人审查通过".equals(wdoi.getRole()) || "技术负责人审查退回".equals(wdoi.getRole())) {
				if("整改后直接使用".equals(wdoi.getState())) {
					WDOpinionInfo wdois = new WDOpinionInfo();
					wdois.setDataGuid(dataGuid);
					wdois.setGuid(wdoi.getGuid());
					wdois.setCheckCount(wdoi.getCheckCount());
					WDOpinionInfo wdoa = conDrawingMapper.selectWDOpinionInfoByPreviousStateGuid(wdois);
					if(!StringUtils.isNull(wdoa) && !StringUtils.isEmpty(wdoa.getOperateDate()) && !StringUtils.isEmpty(wdoi.getOperateDate())) {
						wdoi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(wdoa.getOperateDate()), DateUtil.parseDateTime(wdoi.getOperateDate()), DateUnit.DAY)));
					}else {
						wdoi.setCauseContent("0");
					}
				}else {
					WDOpinionInfo wdois = new WDOpinionInfo();
					wdois.setDataGuid(dataGuid);
					wdois.setCheckCount(wdoi.getCheckCount());
					String operateDate = conDrawingMapper.selectWDOpinionInfoMaxTime(wdois);
					if(!StringUtils.isEmpty(operateDate) && !StringUtils.isEmpty(wdoi.getOperateDate())) {
						wdoi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(operateDate), DateUtil.parseDateTime(wdoi.getOperateDate()), DateUnit.DAY)));
					}else {
						wdoi.setCauseContent("0");
					}
				}
			}else if("技术性整改上报".equals(wdoi.getRole())) {
				WDOpinionInfo wdois = new WDOpinionInfo();
				wdois.setDataGuid(dataGuid);
				wdois.setRole("技术负责人审查退回");
				WDOpinionInfo wdoa = conDrawingMapper.selectWDOpinionInfoByPreviousStateGuid(wdois);
				if(!StringUtils.isNull(wdoa) && !StringUtils.isEmpty(wdoa.getOperateDate()) && !StringUtils.isEmpty(wdoi.getOperateDate())) {
					wdoi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(wdoa.getOperateDate()), DateUtil.parseDateTime(wdoi.getOperateDate()), DateUnit.DAY)));
				}else {
					wdoi.setCauseContent("0");
				}
			}else if("整改上报退回".equals(wdoi.getRole())) {
				WDOpinionInfo result = rectReportReturn(wdoiList,wdoi);
				if(StringUtils.isNotNull(result)) {
					wdoi.setCauseContent(String.valueOf(DateUtil.between(DateUtil.parseDateTime(result.getOperateDate()), DateUtil.parseDateTime(wdoi.getOperateDate()), DateUnit.DAY)));
				}else {
					wdoi.setCauseContent("0");
				}
			}else {
				wdoi.setCauseContent("0");
			}
		}
		return wdoiList;
	}

	@Override
	public ConDrawing selectWdWorkDrawingByGuid(String guid) {
		// TODO Auto-generated method stub
		return conDrawingMapper.selectWdWorkDrawingByGuid(guid);
	}
	
	private WDOpinionInfo rectReportReturn(List<WDOpinionInfo> list,WDOpinionInfo opinion) {
		WDOpinionInfo result = null;
		for(WDOpinionInfo item:list) {
			//后面时间减去前面时间
			if(DateUtil.between(DateUtil.parse(opinion.getOperateDate()), DateUtil.parse(item.getOperateDate()), DateUnit.MS, false)<0) {
				if(result != null) {
					if(DateUtil.between(DateUtil.parse(result.getOperateDate()), DateUtil.parse(item.getOperateDate()), DateUnit.MS, false)>0) {
						result = item;
					}
				}else {
					result = item;
				}
			}
		}
		return result;
	}

	@Override
	public WDOpinionInfo selectWDOpinionInfoByGuid(String guid) {
		// TODO Auto-generated method stub
		return conDrawingMapper.selectWDOpinionInfoByGuid(guid);
	}

	@Override
	public ProReviewPassRefuse ProReviewPassRefuseDetail(String guid) {
		WDOpinionInfo wdoi = conDrawingMapper.selectWDOpinionInfoByGuid(guid);
		ProReviewPassRefuse prpr = new ProReviewPassRefuse();
		prpr.setCauseContent(wdoi.getCauseContent());
		OpinionCauseContent occ = new OpinionCauseContent();
		occ.setNo("No");
		occ.setName("Name");
		occ.setValue("value");
		String nodePath = "/PsDataPacket/FDataList/ProceduralCheakOpininon";
		List<OpinionCauseContent> occList = XmlReadUtil.getOpinionCauseContentXml(wdoi.getOpinionConent(), nodePath, occ);
		prpr.setProceduralCheakOpininon(occList);
		return prpr;
	}

	
	@Override
	public String selectDesignUnitByUnitEngineeringInfoGuid(String unitEngineeringInfoGuid) {
		// TODO Auto-generated method stub
		return constructionPermitMapper.selectDesignUnitByUnitEngineeringInfoGuid(unitEngineeringInfoGuid);
	}

	@Override
	public List<ConDrawFirstTrial> selectUnitEngineeringInfoByProjectGuid(String projectGuid) {
		// TODO Auto-generated method stub
		return projectArchivesBaseInfoMapper.selectUnitEngineeringInfoByProjectGuid(projectGuid);
	}

	@Override
	public List<WDOpinionInfo> selectWDOpinionInfoByPGuid(String pGuid) {
		// TODO Auto-generated method stub
		return conDrawingMapper.selectWDOpinionInfoByPGuid(pGuid);
	}
}
